import { ArticleCommentResponse, ArticleDetailResponse } from "@/types/data"; // 导入文章详情响应类型
import { RootThunkAction } from "@/types/store"; // 导入根 thunk action 类型
import { http } from "@/utils/http"; // 导入 http 请求工具
// 导入 dayjs 相关包
import dayjs from "dayjs"; // 导入 dayjs
import localizedFormat from "dayjs/plugin/localizedFormat"; // 导入 localizedFormat 插件
dayjs.extend(localizedFormat); // 使用 localizedFormat 插件

// 获取文章详情的异步 action
export const getArticleById = (id: string): RootThunkAction => {
  return async (dispatch) => {
    // 发送 HTTP GET 请求获取文章详情
    const res = await http.get<ArticleDetailResponse>(`/articles/${id}`);
    // 提取文章详情数据
    const detail = res.data.data;
    // 将文章详情数据格式化日期并派发到 store 中
    dispatch({
      type: "article/get", // action 类型
      payload: {
        ...detail, // 文章详情数据
        // 格式化日期为中文格式
        pubdate: dayjs(detail.pubdate).locale("zh-cn").format("LL"),
      },
    });
  };
};
// 获取文章评论 - 覆盖数据
export const getArticleComments = (
  type: string,
  id: string
): RootThunkAction => {
  // console.log(type, id,'1235');

  return async (dispatch) => {
    const res = await http.get<ArticleCommentResponse>("/comments", {
      params: {
        type,
        source: id,
      },
    });
    // console.log(res);
    //提取评论数据
    const comments = res.data.data;
    // console.log(comments);

    dispatch({
      type: "article/comments", // action 类型
      payload: comments, //评论数据
    });
  };
};

// 获取更多文章评论数据 - 追加数据
export const getMoreArticleComments = (
  type: string,
  id: string,
  offset?: string | null
): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticleCommentResponse>("/comments", {
      params: {
        type,
        source: id,
        offset,
        limit: 10,
      },
    });
    dispatch({
      type: "article/comments", // action 类型
      payload: res.data.data, //评论数据
    });
  };
};

// 关注作者的异步 action
export const followAuthor = (
  id: string,
  isFollowed: boolean
): RootThunkAction => {
  return async (dispatch) => {
    if (isFollowed) {
      // 如果已关注，执行取关操作
      await http.delete(`/user/followings/${id}`);
    } else {
      // 如果未关注，执行关注操作
      await http.post("/user/followings", {
        target: id,
      });
    }

    // 派发更新作者关注状态的 action
    dispatch({
      type: "article/updateInfo", // action 类型
      payload: {
        name: "is_followed", // 要更新的状态名称
        value: !isFollowed, // 新的状态值
      },
    });
  };
};

// 收藏文章的异步 action
export const collectArticle = (
  art_id: string,
  isCollected: boolean
): RootThunkAction => {
  let body = { target: art_id }; // 收藏请求的请求体
  return async (dispatch) => {
    // 判断当前是否收藏
    if (isCollected) {
      // 如果已收藏，执行取消收藏操作
      await http.delete(`/article/collections/${art_id}`);
    } else {
      // 如果未收藏，执行收藏操作
      await http.post("/article/collections", body);
    }

    // 派发更新收藏状态的 action
    dispatch({
      type: "article/updateInfo", // action 类型
      payload: {
        name: "is_collected", // 要更新的状态名称
        value: !isCollected, // 新的状态值
      },
    });
  };
};

// 点赞文章的异步 action
export const likeArticle = (
  art_id: string,
  attitude: number
): RootThunkAction => {
  return async (dispatch) => {
    // 判断当前是否已点赞
    if (attitude === 1) {
      // 如果已点赞，执行取消点赞操作
      await http.delete(`/article/likings/${art_id}`);
    } else {
      // 如果未点赞，执行点赞操作
      await http.post("/article/likings", {
        target: art_id,
      });
    }

    // 派发更新点赞状态的 action
    dispatch({
      type: "article/updateInfo", // action 类型
      payload: {
        name: "attitude", // 要更新的状态名称
        value: attitude === 1 ? 0 : 1, // 新的状态值
      },
    });
  };
};

// 获取文章评论 - 覆盖数据
const getArticleComment = (type: string, id: string): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<ArticleCommentResponse>("/comments", {
      params: {
        type,
        source: id,
      },
    });
    console.log(res);
  };
};
